home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.01 Jan 91 / Object Design / line feed removal / CRemoveLFApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-26  |  5.4 KB  |  262 lines  |  [TEXT/KAHL]

  1. /*****
  2.  * CRemoveLFApp.c
  3.  *
  4.  *    Application methods for a typical application.
  5.  *
  6.  *****/
  7. #include "oops.h"
  8. #include "messageDefines.h"
  9. #include "CRemoveLFApp.h"
  10. #include "CRemoveLFDoc.h"
  11. #include "CmyFileManager.h"
  12. #include "CmyDataFile.h"
  13. #include "CTextFilter.h"
  14.  
  15. extern    OSType    gSignature;
  16.  
  17. static char readBuffer[512];
  18. /*..........................................................*/
  19. void CRemoveLFApp::Run(void)
  20. /*
  21.     Name: Wade Maxfield
  22.     Date: November 25, 1989
  23.     Notes:
  24.     Run
  25.     
  26.     we need to do our thing here, not the normal built in
  27.     code work
  28.     Modification History:
  29. */
  30. {
  31. CmyFileManager *FileManager;
  32. SFReply sourceReply;
  33. SFReply destinationReply;
  34. short returnMessage;
  35. CmyDataFile *sourceDataFile;
  36. CmyDataFile *destinationDataFile;
  37. long bytesRead;
  38. CTextFilter *TextFilter;
  39.  
  40.     FileManager = new(CmyFileManager);
  41.     FileManager->ImyFileManager();
  42.     
  43. /* ask for the source file */
  44.     returnMessage = FileManager->GetExistingFile(&sourceReply);
  45.  
  46.     if ( returnMessage == NAK )
  47.         return; /* this application is finished. */
  48.         
  49. /* ask for the destination file. (this creates it also) */
  50.     returnMessage = FileManager->GetNewFile(&destinationReply,'TEXT');
  51.  
  52.     if ( returnMessage == NAK )
  53.         return; /* this application is finished. */
  54.  
  55.     returnMessage = FileManager->OpenFile(&sourceDataFile,&sourceReply);
  56.     
  57.     if ( returnMessage == NAK )
  58.         return;
  59.         
  60.     returnMessage = FileManager->OpenFile(&destinationDataFile,&destinationReply);
  61.  
  62.     if ( returnMessage == NAK )
  63.         return;
  64.     
  65.     /* we are ready for the new text filter object */
  66.     TextFilter = new(CTextFilter);
  67.     TextFilter->ITextFilter(); /* initialize it */
  68.     
  69.     do
  70.         {        
  71.         returnMessage = sourceDataFile->ReadLine(readBuffer,512L,&bytesRead);
  72.         
  73.         if ( returnMessage == NAK )
  74.             break;
  75.         
  76.         /* strip the line feeds */
  77.         TextFilter->StripLineFeeds(readBuffer,&bytesRead);
  78.                 
  79.         returnMessage = destinationDataFile->WriteSome(readBuffer,bytesRead);
  80.         } while(TRUE);
  81.  
  82.  
  83.     FileManager->CloseFile(&sourceDataFile);
  84.     
  85.     FileManager->CloseFile(&destinationDataFile);
  86.     
  87.     TextFilter->Dispose(); /* get rid of the text filter object */
  88. }
  89.  
  90.  
  91.  
  92. /***
  93.  * IRemoveLFApp
  94.  *
  95.  *    Initialize the application. Your initialization method should
  96.  *    at least call the inherited method. If your application class
  97.  *    defines its own instance variables or global variables, this
  98.  *    is a good place to initialize them.
  99.  *
  100.  ***/
  101.  
  102. void CRemoveLFApp::IRemoveLFApp(void)
  103.  
  104. {
  105.     CApplication::IApplication(4, 20480L, 2048L);
  106. }
  107.  
  108.  
  109.  
  110. /***
  111.  * SetUpFileParameters
  112.  *
  113.  *    In this routine, you specify the kinds of files your
  114.  *    application opens.
  115.  *
  116.  *
  117.  ***/
  118.  
  119. void CRemoveLFApp::SetUpFileParameters(void)
  120.  
  121. {
  122.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  123.  
  124.         /**
  125.          **    sfNumTypes is the number of file types
  126.          **    your application knows about.
  127.          **    sfFileTypes[] is an array of file types.
  128.          **    You can define up to 4 file types in
  129.          **    sfFileTypes[].
  130.          **
  131.          **/
  132.  
  133.     sfNumTypes = 1;
  134.     sfFileTypes[0] = 'TEXT';
  135.  
  136.         /**
  137.          **    Although it's not an instance variable,
  138.          **    this method is a good place to set the
  139.          **    gSignature global variable. Set this global
  140.          **    to your application's signature. You'll use it
  141.          **    to create a file (see CFile::CreateNew()).
  142.          **
  143.          **/
  144.  
  145.     gSignature = '????';
  146. }
  147.  
  148.  
  149.  
  150. /***
  151.  * DoCommand
  152.  *
  153.  *    Your application will probably handle its own commands.
  154.  *    Remember, the command numbers from 1-1023 are reserved.
  155.  *    The file Commands.h contains all the reserved commands.
  156.  *
  157.  *    Be sure to call the default method, so you can get
  158.  *    the default behvior for standard commands.
  159.  *
  160.  ***/
  161. void CRemoveLFApp::DoCommand(long theCommand)
  162.  
  163. {
  164.     switch (theCommand) {
  165.     
  166.         /* Your commands go here */
  167.     
  168.         default:    inherited::DoCommand(theCommand);
  169.                     break;
  170.     }
  171. }
  172.  
  173. /***
  174.  * Exit
  175.  *
  176.  *    Chances are you won't need this method.
  177.  *    This is the last chance your application gets to clean up
  178.  *    things like temporary files.
  179.  *
  180.  ***/
  181.  
  182. void CRemoveLFApp::Exit()
  183.  
  184. {
  185.     /* your exit handler here */
  186. }
  187.  
  188.  
  189. /***
  190.  * CreateDocument
  191.  *
  192.  *    The user chose New from the File menu.
  193.  *    In this method, you need to create a document and send it
  194.  *    a NewFile() message.
  195.  *
  196.  ***/
  197.  
  198. void CRemoveLFApp::CreateDocument()
  199.  
  200. {
  201.     CRemoveLFDoc    *theDocument;
  202.     
  203.     theDocument = new(CRemoveLFDoc);
  204.         
  205.         /**
  206.          **    Send your document an initialization
  207.          **    message. The first argument is the
  208.          **    supervisor (the application). The second
  209.          **    argument is TRUE if the document is printable.
  210.          **
  211.          **/
  212.     
  213.     theDocument->IRemoveLFDoc(this, TRUE);
  214.  
  215.         /**
  216.          **    Send the document a NewFile() message.
  217.          **    The document will open a window, and
  218.          **    set up the heart of the application.
  219.          **
  220.          **/
  221.     theDocument->NewFile();
  222. }
  223.  
  224. /***
  225.  * OpenDocument
  226.  *
  227.  *    The user chose Open… from the File menu.
  228.  *    In this method you need to create a document
  229.  *    and send it an OpenFile() message.
  230.  *
  231.  *    The macSFReply is a good SFReply record that contains
  232.  *    the name and vRefNum of the file the user chose to
  233.  *    open.
  234.  *
  235.  ***/
  236.  
  237. void CRemoveLFApp::OpenDocument(SFReply *macSFReply)
  238.  
  239. {
  240.     CRemoveLFDoc    *theDocument;
  241.     
  242.     theDocument = new(CRemoveLFDoc);
  243.         
  244.         /**
  245.          **    Send your document an initialization
  246.          **    message. The first argument is the
  247.          **    supervisor (the application). The second
  248.          **    argument is TRUE if the document is printable.
  249.          **
  250.          **/
  251.     
  252.     theDocument->IRemoveLFDoc(this, TRUE);
  253.  
  254.         /**
  255.          **    Send the document an OpenFile() message.
  256.          **    The document will open a window, open
  257.          **    the file specified in the macSFReply record,
  258.          **    and display it in its window.
  259.          **
  260.          **/
  261.     theDocument->OpenFile(macSFReply);
  262. }